<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        class="filter-item"
        clearable
        @keyup.enter.native="handleFind"
        v-model="keyword"
        placeholder="请输入分类名"
      ></el-input>
      <el-button type="primary" @click="handleFind" icon="el-icon-search">查找</el-button>
      <el-button type="primary" @click="handleAdd" icon="el-icon-edit">添加分类</el-button>
      <el-button type="danger" @click="handleDeleteBatch" icon="el-icon-delete">删除选中</el-button>
    </div>

    <el-table :data="tableData" v-loading="Loading" style="width: 100%" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection"></el-table-column>

      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="分类名" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" min-width="230" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      v-if="dialogFormVisible"
      :visible.sync="dialogFormVisible"
      :before-close="handleDialogClose"
      :close-on-click-modal='false'
      width="600px"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="70px">
        <el-form-item label="分类名" prop="sortName">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialogClose">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getBlogSortList,
  addBlogSort,
  editBlogSort,
  deleteBatchBlogSort,
  blogSortByClickCount,
  blogSortByCite,
  stickBlogSort,
} from "@/api/blogSort";
export default {
  data() {
    return {
      Loading: true,
      multipleSelection: [], //多选，用于批量删除
      keyword: "",
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0, //总数量
      title: "增加分类",
      dialogFormVisible: false, //控制弹出框
      isEditForm: false,
      form: {
        id: null,
        name: null,
      },
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在1到10个字符" },
        ]
      },
    };
  },
  created() {
    this.blogSortList();
  },
  methods: {
    blogSortList() {
      this.Loading = true;
      getBlogSortList().then((response) => {
        this.tableData = response.data.data.result;
        this.total = 1;
        this.Loading = false;
      });
    },
    handleFind() {
      this.blogSortList();
    },
    handleAdd() {
      this.title = "增加分类";
      this.dialogFormVisible = true;
      this.isEditForm = false;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isEditForm) {
            this.$store.dispatch('app/setLoading', true);
            editBlogSort(this.form).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleDialogClose();
                this.blogSortList();
            });
          } else {
            this.$store.dispatch('app/setLoading', true);
            addBlogSort(this.form).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleDialogClose();
                this.blogSortList();
            });
          }
        }
      });
    },
    handleDialogClose() {
      this.form = {
        uid: null,
        content: "",
        sortName: "",
        sort: 0,
      };
      this.dialogFormVisible = false;
    },
    handleDeleteBatch() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: "error",
          message: "请先选中需要删除的内容！",
        });
        return;
      }
      this.$confirm("此操作将把选中的分类删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch('app/setLoading', true);
          var rowIdList = []
          for(var type of this.multipleSelection){
            rowIdList.push(type.id)
          }
          deleteBatchBlogSort(rowIdList).then((response) => {
              this.$message({
                type: "success",
                message: response.data,
              });
            this.blogSortList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row) {
      this.title = "编辑分类";
      this.dialogFormVisible = true;
      this.isEditForm = true;
      this.form = Object.assign(this.form, row);
    },
    handleDelete(row) {
      this.$confirm("此操作将把分类删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var params = [];
          params.push(row.id);
          this.$store.dispatch('app/setLoading', true);
          deleteBatchBlogSort(params).then((response) => {
              this.$message({
                type: "success",
                message: response.data,
              });
            this.blogSortList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 改变多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange: function (val) {
      this.currentPage = val;
      this.blogSortList();
    },
  },
};
</script>
